import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/main/login/register/register_logic.dart';
import 'package:share_fit/app/controllers/space/person/person_const.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/app/tools/unit_util.dart';
import 'package:share_fit/generated/locales.g.dart';

class RegisterInfoPage extends StatelessWidget {
  final logic = Get.put(RegisterLogic());

  String name = '';
  String gender = '';
  String date = '';
  int height = -1;
  int weight = -1;
  int steps = -1;

  bool toMainPage = false;

  @override
  Widget build(BuildContext context) {
    if (Get.arguments != null) {
      Map<String, dynamic> params = Get.arguments;
      if (params.containsKey('toMainPage')) {
        toMainPage = params['toMainPage'];
      }
    }
    name = logic.read('space_person_name', defaultValue: '', global: true);
    gender = logic.read('space_person_gender_text', defaultValue: '', global: true);
    date = logic.read('register_info_date', defaultValue: '', global: true);
    height = logic.read(PersonConst.spacePersonHeight, defaultValue: -1, global: true);
    weight = logic.read(PersonConst.spacePersonWeight, defaultValue: -1, global: true);
    steps = logic.read('sport_target_step', defaultValue: -1, global: true);

    return PopScope(
        canPop: false,
        onPopInvoked: (didPop) => logic.goBack(toMainPage),
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
            centerTitle: true,
            leading: BackBtnIcon(onTap: () {
              // Get.offNamed('/login', arguments: {'isFromSpace': false});
              // Get.offAllNamed('/main');
              logic.goBack(toMainPage);
            }),
          ),
          body: Container(
            padding: EdgeInsets.only(left: 42.w, right: 42.w),
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.fromLTRB(72.w, 100.h, 0, 165.h),
                  child: Text(LocaleKeys.person_info.ts,
                      style:
                          TextStyle(fontSize: 90.sp, color: Color(0xFF000013))),
                ),
                _buildItem(
                    Image.asset(
                        "assets/images/space/icon_register_info_name.png"),
                    LocaleKeys.register_info_nickname.ts,
                    name == ''
                        ? LocaleKeys.register_info_input_nickname.ts
                        : name,
                    true,
                    false, onTap: () {
                  Get.offNamed('/login/register/info/name',
                      arguments: {'toMainPage': toMainPage});
                }),
                Divider(
                  height: 1.h,
                ),
                _buildItem(
                    Image.asset(
                        "assets/images/space/icon_register_info_gender.png"),
                    LocaleKeys.person_gender.ts,
                    gender == '' ? LocaleKeys.register_info_gender.ts : gender,
                    false,
                    false, onTap: () {
                  Get.offNamed('/login/register/info/gender',
                      arguments: {'toMainPage': toMainPage});
                }),
                Divider(
                  height: 1.h,
                ),
                _buildItem(
                    Image.asset(
                        "assets/images/space/icon_register_info_date.png"),
                    LocaleKeys.register_info_date.ts,
                    date == '' ? LocaleKeys.register_info_choice.ts : date,
                    false,
                    false, onTap: () {
                  Get.offNamed('/login/register/info/date',
                      arguments: {'toMainPage': toMainPage});
                }),
                Divider(
                  height: 1.h,
                ),
                _buildItem(
                    Image.asset(
                        "assets/images/space/icon_register_info_height.png"),
                    LocaleKeys.person_height.ts,
                    height == -1
                        ? LocaleKeys.register_info_height.ts
                        : '$height${UnitUtil.getLengthUnitName()}',
                    false,
                    false, onTap: () {
                  Get.offNamed('/login/register/info/height',
                      arguments: {'toMainPage': toMainPage});
                }),
                Divider(
                  height: 1.h,
                ),
                _buildItem(
                    Image.asset(
                        "assets/images/space/icon_register_info_weight.png"),
                    LocaleKeys.person_weight.ts,
                    weight == -1
                        ? LocaleKeys.register_info_weight.ts
                        : '$weight${UnitUtil.getWeightUnitName()}',
                    false,
                    false, onTap: () {
                  Get.offNamed('/login/register/info/weight',
                      arguments: {'toMainPage': toMainPage});
                }),
                Divider(
                  height: 1.h,
                ), //
                _buildItem(
                    Image.asset(
                        "assets/images/space/icon_register_info_step.png"),
                    LocaleKeys.register_info_step.ts,
                    steps <= 0
                        ? LocaleKeys.register_info_set_step.ts
                        : steps.toString(),
                    false,
                    true, onTap: () {
                  Get.offNamed('/login/register/info/step',
                      arguments: {'toMainPage': toMainPage});
                }),
                Spacer(),
                Container(
                  height: 136.h,
                  width: 1.sw,
                  margin: EdgeInsets.fromLTRB(42.w, 0, 42.w, 176.h),
                  child: ElevatedButton(
                    onPressed: () {
                      // Get.offAllNamed('/main');
                      logic.goBack(toMainPage);
                    },
                    style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(ColorConst.activeColor),
                        shape: MaterialStateProperty.all(RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)))),
                    child: Text(
                      LocaleKeys.register_info_completed.ts,
                      style: TextStyle(
                        fontSize: 48.sp,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ));
  }

  Widget _buildItem(Image image, title, trailing, isFirst, isLast, {onTap}) {
    var borderRadius = BorderRadius.circular(0);
    if (isFirst && !isLast) {
      borderRadius = const BorderRadius.vertical(top: Radius.circular(10));
    } else if (!isFirst && isLast) {
      borderRadius = const BorderRadius.vertical(bottom: Radius.circular(10));
    } else if (isFirst && isLast) {
      borderRadius = BorderRadius.circular(10);
    }

    Widget widget = Ink(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: borderRadius,
      ),
      child: InkResponse(
          onTap: onTap,
          splashColor: ColorConst.activeColor,
          borderRadius: borderRadius,
          highlightShape: BoxShape.rectangle,
          highlightColor: ColorConst.activeColor,
          containedInkWell: true,
          child: Container(
            height: 160.h,
            child: Row(
              children: [
                Container(
                  margin: EdgeInsets.only(left: 67.w, right: 42.w),
                  width: 54.w,
                  height: 54.w,
                  child: image,
                ),
                Text(title,
                    style: TextStyle(
                        fontSize: 42.sp, color: ColorConst.primaryFontColor)),
                Spacer(),
                Text(trailing,
                    style: TextStyle(
                        fontSize: 42.sp, color: ColorConst.secondaryFontColor)),
                SizedBox(
                  width: 50.w,
                ),
                Icon(
                  Icons.arrow_forward_ios,
                  size: 16,
                ),
                SizedBox(
                  width: 34.w,
                ),
              ],
            ),
          )),
    );
    return widget;
  }
}
